import http from '../apis/api.js';
import util from './util.js';
function getLocateInfo(){
  return new Promise((resole, reject) => {
    location().then(res=>{
      http.getAddressByGeocoder({
        lat: res.latitude,
        lng: res.longitude
      }).then(result => {
       resole({
         city: result.data.addressComponent.city.replace('市', ''),
         lat: res.latitude,
         lng: res.longitude
       })
      })
    },error=>{
      wx.showModal({
          title: '',
          content: '自动定位需要授权地理定位选项',
          confirmText: '去授权',
          success(res) {
            if (res.confirm) {
              wx.openSetting({
                success(res) {
                  getLocateInfo();
                }
              })
            }
          }
        })
    })
  })
}
function location(){
  return new Promise((resolve, reject)=>{
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        resolve(res);
      },
      fail(error){
        reject(error);
      }
    })
  })
}
export default{
  getLocateInfo
}